home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Core / source / apple event.cp < prev    next >
Encoding:
Text File  |  1995-03-16  |  10.5 KB  |  172 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    apple event.h
  3. //    Date:                    7/19/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains declarations related to Apple Event
  7. //                                processing
  8. //
  9. //------------------------------------------------------------------------------
  10.  
  11. #include "menu.h"
  12. #include "apple event.h"
  13.  
  14. //------------------------------------------------------------------------------
  15. //    types
  16. //------------------------------------------------------------------------------
  17. typedef    pascal OSErr (*FileAEProc) (FSSpec &spec);                                                            //    proc type for file AEs
  18.  
  19. //------------------------------------------------------------------------------
  20. //    Check that all parameters have been retrieved from an event
  21. //------------------------------------------------------------------------------
  22. OSErr    CheckGotRequiredParams (const AppleEvent *AE)                                                            //    be sure that I got all of the descriptors
  23. {                                                                                                                                                                //    begin
  24.     DescType    retType;                                                                                                                        //    type of attribute returned
  25.     Size        size;                                                                                                                                    //    how big it really is
  26.     OSErr        myErr = AEGetAttributePtr (AE, keyMissedKeywordAttr,                                    //    get an attribute pointer
  27.                                     typeWildCard, &retType, NULL, 0, &size);                                            //    etc.
  28.     switch (myErr)                                                                                                                                //    examine the return value
  29.     {                                                                                                                                                            //    begin
  30.         case errAEDescNotFound:                                                                                                            //    no more descriptors
  31.             return noErr;                                                                                                                            //    no problem
  32.         case noErr:                                                                                                                                    //    found something
  33.             return errAEParamMissed;                                                                                                    //    I missed something
  34.         default:                                                                                                                                        //    anything else
  35.             return myErr;                                                                                                                            //    problem
  36.     }                                                                                                                                                            //    end
  37. }                                                                                                                                                                //    end
  38.  
  39. //------------------------------------------------------------------------------
  40. //    Extract files from apple event and call the appropriate proc
  41. //------------------------------------------------------------------------------
  42. pascal OSErr FileAE (const AppleEvent *AE, FileAEProc proc)                                            //    handle file AEs (print doc and open doc)
  43. {                                                                                                                                                                //    begin
  44.     AEDescList    docList;                                                                                                                    //    place to get the descriptor list
  45.     OSErr                myErr;                                                                                                                        //    error code
  46.     myErr = AEGetParamDesc (AE, keyDirectObject, typeAEList, &docList);                        //    get the list of file descriptors
  47.     if (myErr) return myErr;                                                                                                            //    bail out if any error
  48.     myErr = CheckGotRequiredParams (AE);                                                                                    //    make sure I got everything
  49.     if (!myErr)                                                                                                                                        //    if there was no error
  50.     {                                                                                                                                                            //    begin
  51.         long    count;                                                                                                                                //    how many items in the file list
  52.         AECountItems (&docList, &count);                                                                                        //    see how many there are
  53.         for (long i = 1; i <= count; i++)                                                                                        //    do for all the items
  54.         {                                                                                                                                                        //    begin
  55.             Size        size;                                                                                                                            //    returned size
  56.             AEKeyword    keywd;                                                                                                                    //    keyword
  57.             DescType    retType;                                                                                                                //    returned type
  58.             FSSpec        mySpec;                                                                                                                    //    FSSpec for target file
  59.             myErr = AEGetNthPtr (&docList, i, typeFSS, &keywd, &retType,                            //    get the file description from the apple event
  60.                             (Ptr) &mySpec, sizeof (FSSpec), &size);                                                        //    get a file spec
  61.             if (myErr) break;                                                                                                                    //    bail out if any error
  62.             myErr = (proc) (mySpec);                                                                                                    //    call the file proc with the spec
  63.             if (myErr) break;                                                                                                                    //    bail out if any error
  64.         }                                                                                                                                                        //    end
  65.     }                                                                                                                                                            //    end
  66.     AEDisposeDesc (&docList);                                                                                                            //    dispose the file list
  67.     return myErr;                                                                                                                                    //    return my error code
  68. }                                                                                                                                                                //    end
  69.  
  70. //------------------------------------------------------------------------------
  71. //    Event handler for start application event
  72. //------------------------------------------------------------------------------
  73. pascal OSErr StartAE (const AppleEvent*, AppleEvent*, long)                                            //    start application event
  74. {                                                                                                                                                                //    begin
  75. /*
  76.     Message ("Graphics Demonstration Application"EOL);
  77.     Message ("Copyright (C) 1994 Bretton Wade"EOL);
  78.     Message ("bw16@cornell.edu"EOL);
  79.     Message ("Cornell University"EOL);
  80.     Message ("Program of Computer Graphics"EOL);
  81.     Message ("All Rights Reserved"EOL EOL);
  82. */
  83.     return noErr;                                                                                                                                    //    no problem
  84. }                                                                                                                                                                //    end
  85.  
  86. //------------------------------------------------------------------------------
  87. //    Individual file proc for handling open document event
  88. //------------------------------------------------------------------------------
  89. pascal OSErr OpenProc (FSSpec&)                                                                                                    //    file proc for opening documents
  90. {                                                                                                                                                                //    begin
  91.     return noErr;                                                                                                                                    //    no problem
  92. }                                                                                                                                                                //    end
  93.  
  94. //------------------------------------------------------------------------------
  95. //    Event handler for open document event
  96. //------------------------------------------------------------------------------
  97. pascal OSErr OpenAE (const AppleEvent *AE, AppleEvent*, long)                                        //    open document event
  98. {                                                                                                                                                                //    begin
  99.     return FileAE (AE, OpenProc);                                                                                                    //    return my result code
  100. }                                                                                                                                                                //    end
  101.  
  102. //------------------------------------------------------------------------------
  103. //    Individual file proc for handling print document event
  104. //------------------------------------------------------------------------------
  105. pascal OSErr PrintProc (FSSpec&)                                                                                                //    file proc for printing documents
  106. {                                                                                                                                                                //    begin
  107.     return noErr;                                                                                                                                    //    no problem
  108. }                                                                                                                                                                //    end
  109.  
  110. //------------------------------------------------------------------------------
  111. //    Event handler for print document event
  112. //------------------------------------------------------------------------------
  113. pascal OSErr PrintAE (const AppleEvent *AE, AppleEvent*, long)                                    //    print document event
  114. {                                                                                                                                                                //    begin
  115.     return FileAE (AE, PrintProc);                                                                                                //    return my result code
  116. }                                                                                                                                                                //    end
  117.  
  118. //------------------------------------------------------------------------------
  119. //    Event Handler for quit event
  120. //------------------------------------------------------------------------------
  121. pascal OSErr QuitAE (const AppleEvent*, AppleEvent*, long)                                            //    quit application event
  122. {                                                                                                                                                                //    begin
  123.     SendToMyself (kFileMenu, kQuitItemEvent);                                                                            //    send myself a quit message
  124.     return noErr;                                                                                                                                    //    no problem
  125. }                                                                                                                                                                //    end
  126.  
  127. //------------------------------------------------------------------------------
  128. //    Install Handlers for My apple events
  129. //------------------------------------------------------------------------------
  130. void    InstallAppleEvents (void)                                                                                                    //    install my apple event handlers
  131. {                                                                                                                                                                //    begin
  132.     AEInstallEventHandler ('aevt', 'oapp', NewAEEventHandlerProc(StartAE), 0, FALSE);//    open application
  133.     AEInstallEventHandler ('aevt', 'odoc', NewAEEventHandlerProc(OpenAE), 0, FALSE);    //    open document
  134.     AEInstallEventHandler ('aevt', 'pdoc', NewAEEventHandlerProc(PrintAE), 0, FALSE);//    print document
  135.     AEInstallEventHandler ('aevt', 'quit', NewAEEventHandlerProc(QuitAE), 0, FALSE);    //    quit application
  136. }                                                                                                                                                                //    end
  137.  
  138. //------------------------------------------------------------------------------
  139. //    Send Apple Event To Myself
  140. //------------------------------------------------------------------------------
  141. void        SendToMyself (AEEventClass AEclass, AEEventID AEmessage)                                //    create an apple event and send it to myself                                                //    create and send a quit message to myself
  142. {                                                                                                                                                                //    begin
  143.     OSErr            myErr;                                                                                                                            //    error condition
  144.     ProcessSerialNumber    myPSN;                                                                                                        //    serial number of this process
  145.     AEAddressDesc        myAddress;                                                                                                        //    address descriptor
  146.     AppleEvent        myAppleEvent;                                                                                                        //    the apple event to send
  147.     myErr = GetCurrentProcess (&myPSN);                                                                                        //    get the serial number for this process
  148.     if (myErr == noErr)                                                                                                                        //    if there was no error
  149.     myErr = AECreateDesc (    typeProcessSerialNumber,                                                            //    create the target description with a process serial number
  150.                         (Ptr) &myPSN,                                                                                                                //    at the PSN for this process
  151.                         sizeof (myPSN),                                                                                                            //    the size of a PSN in bytes
  152.                         (AEDesc *) &myAddress);                                                                                            //    at my address description
  153.     if (myErr == noErr)                                                                                                                        //    if there was no error
  154.     myErr = AECreateAppleEvent (    AEclass,                                                                                //    create the apple event with the event class
  155.                             AEmessage,                                                                                                                //    the message
  156.                             &myAddress,                                                                                                                //    the target address for this application
  157.                             kAutoGenerateReturnID,                                                                                        //    I don't care what my return ID is
  158.                             kAnyTransactionID,                                                                                                 //    this is not part of a transaction
  159.                             &myAppleEvent);                                                                                                        //    at my Apple Event
  160.     if (myErr == noErr)                                                                                                                        //    if there was no error
  161.         myErr = AESend (    &myAppleEvent,                                                                                         //    send my apple event
  162.                         NULL,                                                                                                                                //    with no reply expected
  163.                         kAENoReply + kAECanInteract,                                                                                 //    no reply, but you can ask the user a questio if you need to
  164.                         kAENormalPriority,                                                                                                     //    this is a normal message
  165.                         kNoTimeOut,                                                                                                                 //    I won't be waiting for it to finish
  166.                         NULL,                                                                                                                             //    no idle function
  167.                         NULL);                                                                                                                            //    no filter function
  168.     AEDisposeDesc (&myAddress);                                                                                                        //    clean up when finished
  169. }                                                                                                                                                                //    end
  170.  
  171. //------------------------------------------------------------------------------
  172.